﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GHRMS.APP.Classes;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_HisHealthCheckInfo : UserControl
    {
        private SsMng ssMng = new SsMng();
        private IHisHealthCheckBLL hhcBll = new HisHealthCheckBLL();
        private Main main = new Main();

        protected void Page_Init(object sender, EventArgs e)
        {
            SetComboboxYearData();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            lblTest.Text = "Employee Seq = " + lblEmpSeq.Text;
        }

        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);
            if (!IsUserControlPostBack)
            {
                ViewState.Add("IsUserControlPostBack", true);

                if (Request.QueryString["sta"] != null)
                    lblSta.Text = Request.QueryString["sta"];

                if (Session["emp_seq"] != null && Session["emp_seq"].ToString() != "")
                {
                    lblEmpSeq.Text = Session["emp_seq"].ToString();
                }
                LoadDataGrid();
            }
        }

        public void LoadDataGrid()
        {
            var dataSource = LoadDataSource();

            SetRadGrid(dataSource);
        }

        public IList<HisHealthCheckInfo> LoadDataSource()
        {
            return SelectedRoundType == "Yearly"
                ? hhcBll.ListOfEmployeeByYearly(YearFilter, SelectedEmployeeSeq)
                : hhcBll.ListOfEmployeeByRoundly(YearFilter, SelectedEmployeeSeq);
        }

        protected void RadGridHealth_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete" && e.Item as GridDataItem != null)
            {
                int seq = Convert.ToInt32((e.Item as GridDataItem).
                    OwnerTableView.DataKeyValues[e.Item.ItemIndex]["HealthCheckSequence"]);

                hhcBll.Delete(seq, ssMng.EmpSeqI);
            }

            LoadDataGrid();
        }

        protected void RadGridHealth_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridCommandItem)
            {
                var commandItem = e.Item as GridCommandItem;
                var btnAddNew = commandItem.FindControl("cmdAddNew") as LinkButton;

                if (lblSta.Text == "v" && btnAddNew != null)
                {
                    btnAddNew.Enabled = false;
                    btnAddNew.Visible = true;
                    btnAddNew.Text = "";
                }
            }
            if (e.Item is GridEditFormInsertItem && e.Item.IsInEditMode)
            {
                var insertItem = (GridEditFormInsertItem) e.Item;
                if ((main.FindControlRecursive(insertItem, "emp_seq") as HiddenField) != null)
                    (main.FindControlRecursive(insertItem, "emp_seq") as HiddenField).Value =
                        Session["emp_seq"].ToString();

            }
            Session["round_type"] = (sender as Control).ID;
        }

        protected void rblRoundType_SelectedIndexChanged(object sender, EventArgs e)
        {
            RoundType = rblRoundType.SelectedValue;
            LoadDataGrid();
        }

        protected void cbbYearFilter_SelectedIndexChanged(object sender,
            RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadDataGrid();
        }

        #region Setting ComboboxYear

        private int? YearFilter
        {
            get
            {
                return main.ConvertNullInt(cbbYearFilter.SelectedValue);
            }
        }

        private void SetComboboxYearData()
        {
            string currenctSelected = cbbYearFilter.SelectedValue;
            cbbYearFilter.Items.Clear();
            cbbYearFilter.Items.Add(new RadComboBoxItem("ทั้งหมด", ""));
            int yearStart = new ThaiBuddhistCalendar().GetYear(DateTime.Now) - 20;
            int yearEnd = new ThaiBuddhistCalendar().GetYear(DateTime.Now) + 20;
            for (int i = yearStart; i < yearEnd; i++)
            {
                cbbYearFilter.Items.Add(new RadComboBoxItem(Convert.ToString(i), Convert.ToString(i)));
            }
            SetComboboxYearSelected(currenctSelected);
        }

        private void SetComboboxYearSelected(string selectedValue)
        {
            if (String.IsNullOrEmpty(selectedValue))
                cbbYearFilter.SelectedIndex = cbbYearFilter.Items.FindItemByText("ทั้งหมด").Index;
            else if (cbbYearFilter.Items.FindItemByValue(selectedValue) != null)
                cbbYearFilter.SelectedValue = selectedValue;
            else
                cbbYearFilter.Items.Add(new RadComboBoxItem(selectedValue, selectedValue) {Selected = true});
        }

        #endregion Setting ComboboxYear

        #region Setting DataGrid

        private void SetRadGrid(object dataSource)
        {
            SetRadGridVisibilityByRoundType();
            SetRadGridDataSourceByRoundType(dataSource);
        }

        private void SetRadGridVisibilityByRoundType()
        {
            RadGridHealthYearly.Visible = rblRoundType.SelectedValue == "Yearly";
            RadGridHealthRoundly.Visible = rblRoundType.SelectedValue == "Roundly";
        }

        private void CancelEditYearly()
        {
            foreach (GridDataItem item1 in RadGridHealthYearly.MasterTableView.Items)
            {
                item1.Edit = false;
            }
            RadGridHealthYearly.Rebind();
        }

        private void CancelEditRoundly()
        {
            foreach (GridDataItem item1 in RadGridHealthRoundly.MasterTableView.Items)
            {
                item1.Edit = false;
            }
            RadGridHealthRoundly.Rebind();
        }

        private void SetRadGridDataSourceByRoundType(object dataSource)
        {
            switch (SelectedRoundType)
            {
                case "Yearly":
                    RadGridHealthYearly.DataSource = dataSource;
                    RadGridHealthYearly.Rebind();
                    break;
                case "Roundly":
                    RadGridHealthRoundly.DataSource = dataSource;
                    RadGridHealthRoundly.Rebind();
                    break;
            }
        }

        #endregion Setting DataGrid

        #region Properties

        protected bool IsUserControlPostBack
        {
            get { return ViewState["IsUserControlPostBack"] != null; }
        }

        private int SelectedEmployeeSeq
        {
            get { return Convert.ToInt32(lblEmpSeq.Text); }
        }

        private string SelectedRoundType
        {
            get { return rblRoundType.SelectedValue; }
        }

        private string SelectedYearFilter
        {
            get { return cbbYearFilter.SelectedValue; }
        }

        private string RoundType
        {
            get { return Session["RoundType"].ToString(); }
            set { Session["RoundType"] = value; }
        }

        #endregion Properties
    }
}